package com.open.loan.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.open.loan.domain.req.ReqApplyLogModel;
import com.open.loan.domain.rsp.RspApplyLogListModel;
import com.open.loan.domain.rsp.RspApplyLogModel;
import com.open.loan.utils.ResponseUtil;
import com.open.system.domain.LoanApplyLogDO;
import com.open.system.domain.LoanUserDO;
import com.open.system.service.LoanApplyLogService;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/moblie")
public class VuePostApplyLogController {

	@Autowired
	private LoanApplyLogService applyLogService;
	
	@ApiOperation(value="借款列表", notes="")
	@RequestMapping(value ="/getApplyLoanList",method = RequestMethod.GET)
	public ResponseEntity<String> getApplyLoanList(HttpServletRequest request,HttpServletResponse response){
				HttpSession session = request.getSession();
				LoanUserDO loanUserDo=(LoanUserDO) session.getAttribute("loanUserDo");
				if(loanUserDo==null){
					ResponseUtil.toWrite("100","请重新登录",null, response);
					return null;
				}
				Map<String,Object>mapList=new HashMap<String,Object>();	
				mapList.put("userId",loanUserDo.getId());
				try{
				List<RspApplyLogListModel>models=new ArrayList<RspApplyLogListModel>();
				List<LoanApplyLogDO>applyLogs=applyLogService.list(mapList);
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//这个是你要转成后的时间的格式
					if(applyLogs!=null && applyLogs.size()>0){
						for (int i = 0; i <applyLogs.size(); i++) {
							LoanApplyLogDO logDo=applyLogs.get(i);
							RspApplyLogListModel model=new RspApplyLogListModel();
							model.setCreateTime(sdf.format(logDo.getCreateTime()));
							if("0".equals(logDo.getStatus())){
								model.setStatus("申请中");
							}else if ("1".equals(logDo.getStatus())) {
								model.setStatus("待放款");
							}else if ("2".equals(logDo.getStatus())) {
								model.setStatus("放款中");
							}else if ("3".equals(logDo.getStatus())) {
								model.setStatus("已放款");
							}else if ("4".equals(logDo.getStatus())) {
								model.setStatus("还款中");
							}else if ("5".equals(logDo.getStatus())) {
								model.setStatus("已还清");
							}else if ("6".equals(logDo.getStatus())) {
								model.setStatus("已拒绝");
							}
							model.setId(String.valueOf(logDo.getId()));
							models.add(model);
						}
					}
					ResponseUtil.toWrite("200","获取列表成功",models, response);
					return null;
				} catch (Exception e) {
					e.printStackTrace();
					ResponseUtil.toWrite("202","获取列表失败",null, response);
					return null;
				}
				
		}
	
	
	@ApiOperation(value="借款详情", notes="")
	@RequestMapping(value ="/getApplyLoanInfo",method = RequestMethod.POST)
	public ResponseEntity<ReqApplyLogModel> getApplyLoanInfo(@RequestBody ReqApplyLogModel reqModel,HttpServletRequest request,HttpServletResponse response){
				HttpSession session = request.getSession();
				LoanUserDO loanUserDo=(LoanUserDO) session.getAttribute("loanUserDo");
				if(loanUserDo==null){
					ResponseUtil.toWrite("100","请重新登录",null, response);
					return null;
				}
				Map<String,Object>mapList=new HashMap<String,Object>();	
				mapList.put("userId",loanUserDo.getId());
				mapList.put("id",reqModel.getId());
				try{
				RspApplyLogModel model=new RspApplyLogModel();
				List<LoanApplyLogDO>applyLogs=applyLogService.list(mapList);
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//这个是你要转成后的时间的格式
					if(applyLogs!=null && applyLogs.size()>0){
						for (int i = 0; i <applyLogs.size(); i++) {
							LoanApplyLogDO logDo=applyLogs.get(i);
							model.setProductName(logDo.getProductName());
							model.setApplyAmt(logDo.getApplyAmt());
							model.setLoanAmt(logDo.getLoanAmt());
							model.setCreateTime(sdf.format(logDo.getCreateTime()));
							if(logDo.getLoanTime()!=null){
								model.setLoanTime(sdf.format(logDo.getLoanTime()));
							}
							if(logDo.getRepaymentTime()!=null){
								model.setRepaymentTime(sdf.format(logDo.getRepaymentTime()));
							}
							model.setInterest(logDo.getInterest());
							model.setRemainRepaymentAmt(logDo.getRemainRepaymentAmt());
							model.setAlreadyRepaymentAmt(logDo.getAlreadyRepaymentAmt());
							if("0".equals(logDo.getStatus())){
								model.setStatus("申请中");
							}else if ("1".equals(logDo.getStatus())) {
								model.setStatus("待放款");
							}else if ("2".equals(logDo.getStatus())) {
								model.setStatus("放款中");
							}else if ("3".equals(logDo.getStatus())) {
								model.setStatus("已放款");
							}else if ("4".equals(logDo.getStatus())) {
								model.setStatus("还款中");
							}else if ("5".equals(logDo.getStatus())) {
								model.setStatus("已还清");
							}else if ("6".equals(logDo.getStatus())) {
								model.setStatus("已拒绝");
							}
							model.setLoanStyle(logDo.getRemark1());//还款方式
							model.setPerMonthRepayment("");//每月还款
							model.setTotalNum("");//总期数
							model.setOverdueDays("");//逾期天数
							model.setOverdueAmt("");//逾期金额
							model.setTotalRepaymentAmt(logDo.getTotalRepaymentAmt());
						}
					}
					ResponseUtil.toWrite("200","获取详情成功",model, response);
					return null;
				} catch (Exception e) {
					e.printStackTrace();
					ResponseUtil.toWrite("202","获取详情失败",null, response);
					return null;
				}
				
		}
}
